package com.cloudream.algorithm;

import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.squareup.picasso.Picasso;

import org.json.JSONArray;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    // Used to load the 'native-lib' library on application startup.

    public static final String TAG = "Terry";

    ImageView ivImage;
    ImageView ivRatationImage;

    private byte[] array;

    private Bitmap bitmap ;

    YUVImageTools yuvImageTools ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Example of a call to a native method
        yuvImageTools = new YUVImageTools();

        ivImage = (ImageView)findViewById(R.id.ivImage);
        ivImage.setOnClickListener(this);

        ivRatationImage = (ImageView)findViewById(R.id.ivRatationImage);
        ivRatationImage.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ivImage:
//                doYUV();
                checkBgr();
                break;
            case R.id.ivRatationImage:
                long start = System.currentTimeMillis();

                byte[] data = null;
                File file = new File("/sdcard/ShopGuide/camerm_test/1111.txt");

                RandomAccessFile aFile = null;
                try {
                    aFile = new RandomAccessFile(file.getAbsoluteFile(), "rw");

                    FileChannel inChannel = aFile.getChannel();
                    ByteBuffer allBuffer = ByteBuffer.allocate((int) aFile.length());
                    inChannel.read(allBuffer);
                    data = allBuffer.array();
                } catch (Exception e) {
                    e.printStackTrace();
                }


                    Bitmap rotationBitmap =yuvImageTools.getRotationBitmap(data,270,1920,1080, Bitmap.Config.ARGB_8888);
                Log.e("Terry","rotationBitmap 用时："+ (System.currentTimeMillis() - start));

                ivRatationImage.setImageBitmap(rotationBitmap);
                break;


        }


    }


    private byte[] getBitmapByteArray() throws IOException {
        AssetManager am = getAssets();
        InputStream inputStream = null;
        inputStream = am.open("test.txt");
        int size = inputStream.available();
        byte[] buffer = new byte[size];
        inputStream.read(buffer);
        Log.d(TAG, "length = " + buffer.length);
        inputStream.close();
        return buffer;
    }


    private void checkBgr(){
        try {
            byte[] data =  getBitmapByteArray();
            bitmap = yuvImageTools.getBGRImage(data,1920,1080, Bitmap.Config.ARGB_8888);
            ivImage.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(bitmap != null && !bitmap.isRecycled()){
            bitmap.recycle();
        }

    }

    private void doYUV(){
        //File file = new File(Environment.getExternalStorageDirectory(),"/storage/sdcard0/yuv222.txt");

        File testFile = new File(Environment.getExternalStorageDirectory(),"aaa.txt");

        Log.d("Terry","testFile path:"+testFile.getAbsolutePath());


        //File file = new File("/sdcard/yuv222.txt");
        File file = new File("/sdcard/camaretest/1528771439951_preview_data.txt");
        try {

            RandomAccessFile aFile = new RandomAccessFile(file.getAbsoluteFile(), "rw");
            FileChannel inChannel = aFile.getChannel();
            ByteBuffer allBuffer = ByteBuffer.allocate(1280*960*3/2);
            inChannel.read(allBuffer);
            long time = System.currentTimeMillis();

            //byte[] yuvSecond = yuvImageTools.yuvReduceWidth(allBuffer.array(),1920,1080,640);


            Log.e("Terry","hao shi11 "+(System.currentTimeMillis() - time));

            time = System.currentTimeMillis();
            bitmap = yuvImageTools.getBGRImage(allBuffer.array(),1280,960, Bitmap.Config.ARGB_8888);

            Log.e("Terry","hao shi "+(System.currentTimeMillis() - time));
            inChannel.close();



//            Matrix matrix = new Matrix();
//            matrix.postRotate(-90);
//            Bitmap rotaBitmap = Bitmap.createBitmap(bitmap,0,0,1080,1920,matrix,false);
//
//
//            ByteBuffer buffer =  ByteBuffer.allocate(rotaBitmap.getWidth() * rotaBitmap.getHeight()*4);
//            rotaBitmap.copyPixelsToBuffer(buffer);
//
//            array = buffer.array();

            //
//
////            if(!bitmap.equals(rotaBitmap)){
////                if(!bitmap.isRecycled()){
////                    bitmap.recycle();
////                }
////            }
//
//            //File bitmapfile = new File(Environment.getExternalStorageDirectory(),"pic.jpg");
//            File bitmapfile = new File("/sdcard/pic.jpg");
//            BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(bitmapfile));
//            rotaBitmap.compress(Bitmap.CompressFormat.JPEG,100,outputStream);
//            outputStream.close();
//


            ivImage.setImageBitmap(bitmap);

        } catch (Exception e) {
            Log.e("Terry","error",e);
        }
    }

}
